<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.transcargo.users.Contact" %>
<%@ page import="com.transcargo.users.User" %>
<%@ page import="com.transcargo.users.Transport" %>
<%@ page import="com.transcargo.errors.ErrorList" %>
<%@ page import="com.transcargo.data.CountriesData" %>
<%@ page import="com.transcargo.geo.Country" %>
<%@ page import="com.transcargo.geo.Region" %>
<%@ page import="com.transcargo.geo.City" %>
<%@ page import="com.transcargo.util.Normalizer" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.StringTokenizer" %>
<%@include file="header.jsf"%>
<%@include file="session.jsf"%>
<%@include file="resolve_country.jsf"%>
<%
    if (User_Id <= 0) {
        response.sendRedirect("auth.jsp");
        return;
    }
    Country[] countries = CountriesData.getCountries(dbase);
	String id = request.getParameter("id");
	if (id == null || id.length() == 0 || Integer.parseInt(id) == 0) {
		response.sendRedirect("posts.jsp");
		return;
	}
	HashMap<String,String> loads = null;
	String datetype = "";
	String date_from = "";
	String date_to = "";
	String country_id_from = "";
	String region_id_from = "";
	String city_id_from = "";
	String country_id_to = "";
	String region_id_to = "";
	String city_id_to = "";
	String city_value_from = "";
	String city_value_to = "";
	String cargo = "";
	String cars = "";
	String cargo_type = "";
	String volume = "";
	String weight = "";
	String tir = "";
	String adr = "";
	String payment_type = "";
	String bid = "";
	String payment_curr = "";
	String urgent = "";
	String urgent_time = "";
	String passing = "";
	String comments = "";
	String combined = "";
	String customs = "";
	String loading = "";
	String times = "";
	String user_id = "";
	
	loads = new HashMap<String,String>();
	int Id = Integer.parseInt(id);
	list = null;
	list = dbase.execute("SELECT date_load, " +
    		"date_unload, " +
    		"country_id_from, " +
    		"region_id_from, " +
    		"city_id_from, " +
    		"city_value_from, " +
    		"country_id_to, " +
    		"region_id_to, " +
    		"city_id_to, " +
    		"city_value_to, " +
    		"cargo, " +
    		"cars, " +
    		"type, " +
    		"loading, " +
    		"tir, " +
    		"adr, " +
    		"weight, " +
    		"volume, " +
    		"payment_bid, " +
    		"payment_type, " +
    		"payment_currency, " +
    		"urgent, " +
    		"urgent_time, " +
    		"combined, " +
    		"passing, " +
    		"comments, " +
    		"permanent, " + 
    		"times, " +
    		"user_id FROM posted_cargo WHERE id=" + Integer.toString(Id) + " AND (user_id mod 100000000)=" + Integer.toString(User_Id));
	if (list != null) iter = list.iterator();
	if (iter.hasNext()) {
		String[] res = iter.next();
		date_from = res[0];
		date_to = res[1];
		country_id_from = res[2];
		region_id_from = res[3];
		city_id_from = res[4];
		city_value_from = res[5];
		country_id_to = res[6];
		region_id_to = res[7];
		city_id_to = res[8];
		city_value_to = res[9];
		cargo = res[10];
		cars = res[11];
		cargo_type = res[12];
		loading = res[13];
		tir = res[14];
		adr = res[15];
		weight = res[16];
		volume = res[17];
		bid = res[18];
		payment_type = res[19];
		payment_curr = res[20];
		urgent = res[21];
		urgent_time = res[22];
		combined = res[23];
		passing = res[24];
		comments = res[25];
		datetype = res[26];
		times = res[27];
		user_id = res[28];
		
		StringTokenizer tok = new StringTokenizer(loading,",");
		while (tok.hasMoreTokens()) {
			loads.put(tok.nextToken(),"1");				
		}

	} else {
		response.sendRedirect("posts.jsp");
		return;
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  	<head><title>TransCargo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="ajaxrequest.js"></script>
<script type="text/javascript">

var num_countries=<%=countries.length%>;
var Countries=new Array();
var CountryValues=new Array();
<%
    for (int i = 0; i < countries.length; i++) {
        Country country = countries[i];
        out.println("Countries[" + i + "]='" + country.getName() + "';");
        out.println("CountryValues[" + i + "]='" + country.getId() + "';");
    }
    iter = null;
    list = dbase.execute("SELECT max(id) from transport_loading");
    if (list != null) iter = list.iterator();
    if (iter.hasNext()) {
 	   String[] ctr = iter.next();
 	   out.println("var maxLds = " + ctr[0]);
    }
%>
var cid = <%=country_id%>;

</script>
      <script type="text/javascript" src="chooseareas.js"></script>
      <script type="text/javascript" src="forms.js"></script>
	<link type="text/css" href="transcargo.css" rel="stylesheet"/>
  </head>
  <body onload="initTransport();">
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr><td>
			<%@include file="page_header.jsf"%>
        </td></tr>
        <tr><td align="center">
            Изменение объявления о свободном грузе<br><br>
			<div id="ErrorPlaceHolder" class="errorString"><br><br></div>
            <form action="EditPostCargoAction" method="POST" target="ifPost">
			<input type="hidden" name="id" value="<%=Id %>">
            <table border="0" cellpadding="0" cellspacing="0" width="490">
                <tr class="tdFormSearch" valign="middle"><td><b>Дата загрузки:</b></td><td align="left" valign="middle"><input type="radio" name="datetype" value="on"<% if (datetype.equals("0")) out.print(" checked"); %>> с <input name="date_from" id="date_from" value="<%=Normalizer.DenormalizeDateFull(date_from)%>" size="8" onfocus="gfPop.fPopCalendar(document.getElementById('date_from'));return false;"> по <input name="date_to" id="date_to" value="<%=Normalizer.DenormalizeDateFull(date_to)%>" size="8" onfocus="gfPop.fPopCalendar(document.getElementById('date_to'));return false;"></td></tr>
                <tr class="tdFormSearch"><td>&nbsp;</td><td align="left"><input type="radio" name="datetype" value="off"<% if (datetype.equals("1")) out.print(" checked"); %>>Постоянно, <input name="times" size="2" value="<%=times %>"> раз(а) в неделю</td></tr>
                <tr class="tdFormSearch"><td><b>Место загрузки:</b></td><td align="left"><input name="from" id="from" size="20" onfocus="toggleFrom(true)" onblur="toggleFrom(false)" onChange="changeFrom()" onkeyup="changeFrom()" value="<%=city_value_from%>">
						(<a id="FRegionPlace">укажите пункт</a>)
						<input type="hidden" name="regfrom" id="regFrom"><br>
						<div id="FromPlaceHolder" style="position: absolute;visibility:hidden;z-index: 999;"></div></td></tr>
                <tr class="tdFormSearch"><td><b>Место выгрузки:</b></td><td align="left"><input name="to" id="to" size="20" onfocus="toggleTo(true)" onblur="toggleTo(false)" onChange="changeTo()" onkeyup="changeTo()" value="<%=city_value_to%>">
						(<a id="TRegionPlace">любое направление</a>)
						<input type="hidden" name="regto" id="regTo"><br>
						<div id="ToPlaceHolder" style="position: absolute;visibility:hidden;z-index: 999;"></div></td></tr>
				<tr><td class="tdFormSearch"><b>Груз:</b></td><td align="left"><input name="cargo" size="20" value="<%=cargo%>"></td></tr>
                <tr><td colspan="2"><hr></td></tr>
                <tr><td class="tdFormSearch"><b>Тип транспорта:</b></td><td align="left"><select name="cargo_type" id="cargo_type" class="selNormal">
					<%
					        iter = null;
					        list = dbase.execute("SELECT id, name from transport_type");
					        if (list != null) iter = list.iterator();
					        if (iter != null) {
					            while (iter.hasNext()) {
					                String[] str = iter.next();
									if (cargo_type.equals(str[0])) {
					                %><option value="<%=str[0]%>" selected><%=str[1]%></option><%
									} else {
					                %><option value="<%=str[0]%>"><%=str[1]%></option><%
									}
					            }
					        }
					%>
				    </select></td></tr>
                <tr><td class="tdFormSearch"><b>Тип загрузки:</b></td><td align="left">
                    <%
				        iter = null;
				        list = dbase.execute("SELECT id, name from transport_loading");
				        if (list != null) iter = list.iterator();
			            while (iter.hasNext()) {
			                String[] str = iter.next();
							if (loads.containsKey(str[0])) {
			                %><input type="checkbox" name="load<%=str[0]%>" checked><%=str[1]%><br><%
							} else {
			                %><input type="checkbox" name="load<%=str[0]%>"><%=str[1]%><br><%
								
							}
			            }
				%>
                </td></tr>
                <tr class="tdFormSearch"><td><b>Вес:</b></td><td align="left"><input name="weight" size="10" value="<%=weight %>"> т.</td></tr>
                <tr class="tdFormSearch"><td><b>Объем:</b></td><td align="left"><input name="volume" size="10" value="<%=volume %>"> м<sup>3</sup></td></tr>
                <tr class="tdFormSearch"><td>TIR Carnet:</td><td align="left"><input type="checkbox" name="tir"<% if (tir.equals("1")) out.print(" checked"); %>></td></tr>
                <tr class="tdFormSearch"><td>Класс ADR:</td><td align="left"><select name="adr">
                    <option value="">-</option>
                    <option value="1"<% if (adr.equals("1")) out.print(" selected"); %>>1</option>
                    <option value="2"<% if (adr.equals("2")) out.print(" selected"); %>>2</option>
                    <option value="3"<% if (adr.equals("3")) out.print(" selected"); %>>3</option>
                    <option value="4"<% if (adr.equals("4")) out.print(" selected"); %>>4</option>
                    <option value="5"<% if (adr.equals("5")) out.print(" selected"); %>>5</option>
                    <option value="6"<% if (adr.equals("6")) out.print(" selected"); %>>6</option>
                    <option value="7"<% if (adr.equals("7")) out.print(" selected"); %>>7</option>
                    <option value="8"<% if (adr.equals("8")) out.print(" selected"); %>>8</option>
                    <option value="9"<% if (adr.equals("9")) out.print(" selected"); %>>9</option>
                </select></td></tr>
			    <tr><td class="tdFormSearch">Таможенный контроль:</td><td align="left"><input type="checkbox" name="customs"<% if (customs.equals("1")) out.print(" checked"); %>></td></tr>
			    <tr><td class="tdFormSearch">Количество машин:</td><td align="left"><input size="4" name="cars" value="<%=cars%>"></td></tr>
                <tr><td colspan="2"><hr></td></tr>
                <tr class="tdFormSearch"><td>Форма оплаты:</td><td align="left"><select name="payment_type" id="payment_type" class="selNormal">
					<%
					        iter = null;
					        list = dbase.execute("SELECT id, name from payment_types");
					        if (list != null) iter = list.iterator();
				            while (iter.hasNext()) {
				                String[] str = iter.next();
								if (payment_type.equals(str[0])) {
				                %><option value="<%=str[0]%>" selected><%=str[1]%></option><%
								} else {
				                %><option value="<%=str[0]%>"><%=str[1]%></option><%
				            	}
				            }
					%>
				    </select></td></tr>
                <tr class="tdFormSearch"><td>Ставка:</td><td align="left"><input name="bid" size="5" value="<% if (!bid.equals("0")) out.print(bid); %>"><select name="payment_curr" id="payment_curr" class="selShort">
				<%
					        iter = null;
					        list = dbase.execute("SELECT id, name from payment_currencies");
					        if (list != null) iter = list.iterator();
				            while (iter.hasNext()) {
				                String[] str = iter.next();
								if (payment_curr.equals(str[0])) {
				                %><option value="<%=str[0]%>" selected><%=str[1]%></option><%
								} else {
				                %><option value="<%=str[0]%>"><%=str[1]%></option><%
								}
				            }
					%>
					<option value="-"<% if (payment_curr.equals("0")) out.print(" selected"); %>>Договорная</option>
				</select></td></tr>
                <tr class="tdFormSearch"><td>&nbsp;</td><td align="left"><input type="checkbox" name="urgent"<% if (urgent.equals("1")) out.print(" checked"); %>><b>Срочно! (загрузить до <input name="urgenttime" size="5" value="<%=urgent_time %>">)</b></td></tr>
                <tr class="tdFormSearch"><td>&nbsp;</td><td align="left"><input type="checkbox" name="combined"<% if (combined.equals("1")) out.print(" checked"); %>>Сборный груз</td></tr>
                <tr class="tdFormSearch"><td>&nbsp;</td><td align="left"><input type="checkbox" name="passing"<% if (passing.equals("1")) out.print(" checked"); %>>Попутный транспорт</td></tr>
                <tr class="tdFormSearch"><td>Примечание:</td><td align="left"><input name="comments" size="30" value="<%=comments %>"></td></tr>
<% 
	if (user.getNumContacts() > 0) {
		int contact_id = Integer.parseInt(user_id) / 100000000;
%>
                <tr><td colspan="2"><hr></td></tr>
                <tr class="tdFormSearch"><td>Контакт:</td><td align="left"><select name="contact" class="selNormal"><option value="">Общий (<%=user.getName() %>)</option>
<%
	Contact[] cts = user.getContacts();
	for (int i = 0; i < cts.length; i++) if (contact_id == cts[i].getId())
		out.println("<option value=\"" + cts[i].getId() + "\" selected>" + cts[i].getName() + "</option>"); else
		out.println("<option value=\"" + cts[i].getId() + "\">" + cts[i].getName() + "</option>");
}
%>                <tr><td class="tdFormSearch"><input type="submit" value="Продолжить"></td><td align="left"><input type="submit" value="Отмена" onclick="gouser();return false;"></td></tr>
            </table>
            </form>
        </td></tr>
        </table>
	<iframe name="ifPost" id="ifPost" width="0" height="0" style="visibility: hidden;width: 0;height: 0;" onload="handlePost(this);" onload="handlePost(this);"></iframe>
	<iframe width=174 height=189 name="gToday:cal:none" id="gToday:cal:none" src="calendar/cal.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;"></iframe>
<script type="text/javascript">
	var ef = document.getElementById('FromPlaceHolder');
	var et = document.getElementById('ToPlaceHolder');
	AddSearchArea('From',ef,<%=country_id_from%>,<%=region_id_from%>,<%=city_id_from%>);
	AddSearchArea('To',et,<%=country_id_to%>,<%=region_id_to%>,<%=city_id_to%>);
</script>
  </body>
</html>
